﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmChangePassword : BaseForm
    {
        #region Starting Form
        public FrmChangePassword()
        {
            InitializeComponent();
        }
        private void FrmChangePassword_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                if (Program.LoggedUser != null)
                    txtLogin.Text = Program.LoggedUser.Login;
                else
                {
                    MessageBox.Show("Usuário não autenticado!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                HandleException("Inicializar a tela!", ex);
            }
        }
        #endregion

        #region Events
        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            ConfirmPasswordChange();
        }
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            CancelPasswordChange();
        }
        #endregion

        #region Methods
        private void ConfirmPasswordChange()
        {
            try
            {
                if (ValidateForm())
                {
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    BusinessEntity.UserManagement.User user = bl.AuthenticateUser(Program.LoggedUser.Login, GTA.Cryptography.Util.DESCryptographer.Encrypt(txtOldPassword.Text));
                    if (user != null)
                    {
                        bl.ChangeUserPassword(Program.LoggedUser.ID, GTA.Cryptography.Util.DESCryptographer.Encrypt(txtNewPassword.Text));
                        MessageBox.Show("Senha atualizada com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Tag = true;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Usuário ou senha inválido!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtOldPassword.Text = String.Empty;
                        txtOldPassword.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Atualizar a senha!", ex);
            }
        }
        private bool ValidateForm()
        {
            bool valid = true;
            StringBuilder message = new StringBuilder("Preencha todos os campos obrigatórios:\r\n");
            if (String.IsNullOrEmpty(txtNewPassword.Text.Trim()))
            {
                valid = false;
                message.Append("\r\nNova Senha");
            }
            if (String.IsNullOrEmpty(txtNewPasswordConfirmation.Text.Trim()))
            {
                valid = false;
                message.Append("\r\nConfirmação da Nova Senha");
            }
            if (String.IsNullOrEmpty(txtOldPassword.Text.Trim()))
            {
                valid = false;
                message.Append("\r\nSenha Antiga");
            }
            if (String.Compare(txtNewPassword.Text.Trim(), txtNewPasswordConfirmation.Text.Trim()) != 0)
            {
                valid = false;
                message.Append("\r\nConfirmação de senha inválida! Digite a senha novamente");
                txtNewPasswordConfirmation.Text = String.Empty;
                txtNewPasswordConfirmation.Focus();
            }
            if (!valid)
                MessageBox.Show(message.ToString(), "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return valid;
        }
        private void CancelPasswordChange()
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                HandleException("Cancelar a atualização da senha!", ex);
            }
        }
        #endregion
    }
}
